class Goods {
    time = 500;
    goods = [
        {
            name: '苹果',
            price: '10',
            inventory: "99"
        },
        {
            name: '桃子',
            price: '12',
            inventory: "88"
        }
    ];

    getGoods() {
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve(this.goods);
            }, this.time);
        });
    }

    createGoods(params) {
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                if (!params) {
                    reject("不能不传");
                    return;
                }
                this.goods.push(params);
                resolve("成功");
            }, this.time);
        });
    }

    delGoods(index) {
        return new Promise((resolve, reject) => {
            if (typeof index !== 'number') {
                reject('必须传数值')
            }
            this.goods.splice(index, 1)
            resolve()
        })
    }

    updateGoods(index, data) {
        return new Promise((resolve, reject) => {
            if (typeof index !== 'number') {
                reject('必须传数值')
            }
            this.goods[index] = data
            resolve()
        })
    }

    getGoodsDetail(index) {
        return new Promise((resolve, reject) => {
            if (typeof index !== 'number') {
                reject('必须传数值')
            }
            resolve(this.goods[index])
        })
    }
}

export const goods = new Goods();
